package 比特教务管理系统.双链表;

import java.util.HashSet;
import java.util.Set;

public class 第六题 {
    public static MyDoubleLinkedList.Node deleteDuplication(MyDoubleLinkedList.Node pHead){
        if (pHead == null){
            return null;
        }
        MyDoubleLinkedList.Node res = new MyDoubleLinkedList.Node(0);
        //加一个表头

        res.next = pHead;
        MyDoubleLinkedList.Node cur = res;
        while (cur.next != null && cur.next.next != null){
            if (cur.next.data == cur.next.next.data){
                int temp = cur.next.data;
                while (cur.next != null && cur.next.data == temp){
                    cur.next = cur.next.next;
                }
            }else {
                cur = cur.next;
            }
        }
        //返回时去掉表头
        return res.next;
    }
    public static MyDoubleLinkedList.Node 删除链表重复节点(MyDoubleLinkedList myDoubleLinkedList){
        Set<MyDoubleLinkedList.Node> set = new HashSet<>();
        MyDoubleLinkedList.Node cur = myDoubleLinkedList.getHead();
        while (cur != null){
            if (!set.contains(cur)){
                set.add(cur);
            }else {
                myDoubleLinkedList.deleteAllKey(cur.data);
            }
            cur = cur.next;
        }
        return myDoubleLinkedList.getHead();
    }

    public static void main(String[] args) {
        MyDoubleLinkedList myDoubleLinkedList = new MyDoubleLinkedList();
        myDoubleLinkedList.insertLast(1);
        myDoubleLinkedList.insertLast(3);
        myDoubleLinkedList.insertLast(3);
        myDoubleLinkedList.insertLast(5);
        myDoubleLinkedList.insertLast(7);
        myDoubleLinkedList.insertLast(9);
        myDoubleLinkedList.insertLast(9);
        myDoubleLinkedList.displayAll();
        MyDoubleLinkedList.Node current = 删除链表重复节点(myDoubleLinkedList);
        while (current != null){
            current.displayNode();
            current = current.next;
        }
    }
}
